#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         Entity.py
#
# Copyright:    Copyright (c) 2006, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  A KnowledgeEngine 'Entity' encapsulates the attributes
#               for a single entity provided by an EntitySource
#
# See Also:     EntitySource
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Common import *

#####################################################################
## Zope Library Imports
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from AccessControl.Role import RoleManager
 
#####################################################################
## Python Library Imports
import types

#####################################################################
## Entity Class Definition
class Entity(RoleManager):
    """
        An object to hold transitent Entity information.
        
        Created by an EntitySource
    """


    #---------------------------------------------------------------#
    # Declare security so ClassInfo may be accessed in DTML/ZPT etc
    security = ClassSecurityInfo()
    security.declareObjectPublic()
    security.setDefaultAccess("allow")


    isEntity = True

    #---------------------------------------------------------------#
    def __init__(self, identity, title, data = None):
        """
            All entities have an identity and a displayable title.
        """
        
        assert type(identity) in types.StringTypes or type(identity) is types.IntType, "Unknown identity type: %s" % `identity`
        
        self._identity   = identity
        self._title      = title
        self._data       = data


    #---------------------------------------------------------------#
    def getIdentity(self):
        """
            Returns the unique identity for the entity
            
            RETURN TYPE: String
        """
        
        return self._identity


    #---------------------------------------------------------------#
    def getTitle(self):
        """
            Returns the title for the entity
            
            RETURN TYPE: String
        """
        
        return self._title


    #---------------------------------------------------------------#
    def setTitle(self, title):
        """
            Sets the title of the entity
        """
        
        if self._title != title:
            self._title = title
            

    #---------------------------------------------------------------#
    def getData(self):
        """
            Returns the entity data.
            
            RETURN TYPE: Dictionary
        """

        return self._data


    #---------------------------------------------------------------#
    
    def isInvalid(self):
        """
            True if the entity no longer exists
        """

        return False

    #---------------------------------------------------------------#

    def __str__(self):
        """ When serialized, we just want to return this entities identity """
        
        return self.getIdentity()


#####################################################################
## DummyEntity Class Definition
class DummyEntity(Entity):
    """
        A model entity i.e. assessment
    """

    #---------------------------------------------------------------#
    # Declare security so ClassInfo may be accessed in DTML/ZPT etc
    security = ClassSecurityInfo()
    security.declareObjectPublic()
    security.setDefaultAccess("allow")


    #---------------------------------------------------------------#
    def isInvalid(self):
        """
            A DummyEntity doesn't actually exist.
        """

        return True


#####################################################################
## Class Initialisation
InitializeClass(Entity)
